import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/home'
import Orders from '../views/orders.vue'
import Center from '../views/center.vue'
import Share from '../views/share.vue'
import Login from '../views/login'
import SmsCode from '../views/login/sms'
import Result from '../views/result'

import store from '@/store'
import { noop } from '@/utils'

const routes = [
	{
		path: '/result',
		name: 'result',
		component: Result,
		meta: {
			title: '支付成功'
		}
	},
	{
		path: '/',
		name: 'home',
		component: HomeView,
		meta: {
			title: '工作台',
			tabbar: true
		}
	},
	{
		path: '/orders',
		name: 'orders',
		component: Orders,
		meta: {
			title: '展业记录',
			tabbar: true
		}
	},
	{
		path: '/center',
		name: 'center',
		component: Center,
		meta: {
			title: '我的',
			tabbar: true
		}
	},
	{
		path: '/share',
		name: 'share',
		component: Share,
		meta: {
			title: '分享'
		}
	},
	{
		path: '/login',
		name: 'Login',
		component: Login,
		meta: {
			title: '登录'
		}
	},
	{
		path: '/smsCode',
		name: 'smsCode',
		component: SmsCode,
		meta: {
			title: '短信验证'
		}
	}
	// {
	// 	path: '/feedback',
	// 	name: 'feedback',
	// 	component: () => import('../views/feedback.vue'),
	// 	meta: {
	// 		title: '问题反馈'
	// 	}
	// }
]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})

const whiteList = ['/login', '/smsCode']

router.beforeEach(async (to, from, next) => {
	// if (to.query.distributorId && !whiteList.includes(to.path)) {
	// 	localStorage.clear()
	// 	next('/login?distributorId=' + to.query.distributorId)
	// }

	// if (whiteList.includes(to.path)) {
	// 	next()
	// 	return
	// }

	// const { userInfo, token } = store.getters
	// if (!userInfo && token) {
	// 	await store.dispatch('getUserInfo').catch(noop)
	// }
	// if (!store.getters.userInfo) {
	// 	next('/login')
	// } else {
	// 	to.path == '/login' ? next('/') : next()
	// }

	next()
})

router.afterEach((to) => {
	// if (!whiteList.includes(to.path) && !store.getters.userInfo.name) {
	// 	store.dispatch('showUserForm')
	// }
})

export default router
